package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.device.idea.meeting.Account.model.H323ServerInfo;
import com.device.idea.meeting.Account.model.H323ServerInfoBase;
import com.device.idea.meeting.Account.model.RegisterState;
import com.device.idea.meeting.Account.model.SIPServerInfo;
import com.device.idea.meeting.Account.model.SIPServerInfoBase;
import com.device.idea.meeting.MultiStream.model.LayoutMode;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.StringUtil;

public class ConfSettingDialog extends Dialog {

    private Context mContext;
    private TextView sipState;
    private TextView h323State;
    private ImageView sipMore;
    private ImageView h323More;
    private ImageView exitDialog;
    private RelativeLayout undisturbView;
    private Switch undisturbSwitch;
    private RelativeLayout sipRegisterDetail;
    private RelativeLayout h323RegisterDetail;
    private EditText sipServerAddress;
    private EditText sipProxyServerAddress;
    private EditText sipUri;
    private EditText sipAccount;
    private EditText sipPassword;
    private RadioGroup sipTransTypeGroup;
    private Button sipRegister;
    private EditText h323GkAddress;
    private EditText h323E164;
    private EditText h323Account;
    private EditText h323Password;
    private Button h323Register;
    private Button flexibleLayoutOpen;
    private Button flexibleLayoutClose;

    public ConfSettingDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        initView();
        initData();
        initListener();
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        super.show();
    }

    private void initView() {
        setContentView(R.layout.dialog_conf_setting);
        sipState = findViewById(R.id.sip_register_state);
        h323State = findViewById(R.id.h323_register_state);
        sipMore = findViewById(R.id.sip_more);
        h323More = findViewById(R.id.h323_more);
        undisturbView = findViewById(R.id.undisturb_view);
        undisturbSwitch = findViewById(R.id.undisturb_switch);
        sipRegisterDetail = findViewById(R.id.sip_register_detail);
        h323RegisterDetail = findViewById(R.id.h323_register_detail);
        exitDialog = findViewById(R.id.conf_setting_exit);
        sipServerAddress = findViewById(R.id.sip_server_address);
        sipProxyServerAddress = findViewById(R.id.sip_proxy_server_addr);
        sipUri = findViewById(R.id.sip_uri);
        sipAccount = findViewById(R.id.sip_account);
        sipPassword = findViewById(R.id.sip_password);
        sipTransTypeGroup = findViewById(R.id.sip_radio_group);
        sipRegister = findViewById(R.id.button_sip_register);
        h323GkAddress = findViewById(R.id.h323_gk_address);
        h323E164 = findViewById(R.id.h323_e164);
        h323Account = findViewById(R.id.h323_account);
        h323Password = findViewById(R.id.h323_password);
        h323Register = findViewById(R.id.button_h323_register);
        flexibleLayoutOpen = findViewById(R.id.flexible_layout_open);
        flexibleLayoutClose = findViewById(R.id.flexible_layout_close);
    }

    private void initListener() {
        sipMore.setOnClickListener(v -> sipMoreClickHandle());
        h323More.setOnClickListener(v -> h323MoreClickHandle());
        exitDialog.setOnClickListener(v -> exitClickHandle());
        h323Register.setOnClickListener(v ->registH323());
        sipRegister.setOnClickListener(v -> registSip());
        undisturbSwitch.setOnCheckedChangeListener((view, isChecked) -> undisturbHandle(view, isChecked));
        flexibleLayoutOpen.setOnClickListener(v -> ConfSdkPresenter.setLayoutMode(LayoutMode.FLEX_LAYOUT));
        flexibleLayoutClose.setOnClickListener(v -> ConfSdkPresenter.setLayoutMode(LayoutMode.FULL_SCREEN));
    }

    private void initData() {
        initConfServer();
        initConfConfig();
    }

    private void initConfServer() {
        SIPServerInfoBase sipServerInfoBase = ConfSdkPresenter.getSIPAccountInfo();
        H323ServerInfoBase h323ServerInfoBase = ConfSdkPresenter.getH323AccountInfo();

        if (sipServerInfoBase != null && sipServerInfoBase.getSipRegState() == RegisterState.REG_STATE_REGISTERED) {
            sipState.setText("已注册");
            sipServerAddress.setText(sipServerInfoBase.getServerAddr());
            sipProxyServerAddress.setText(sipServerInfoBase.getProxyServerAddr());
            sipUri.setText(sipServerInfoBase.getSiteNumber());
            sipAccount.setText(sipServerInfoBase.getUserName());
            sipPassword.setText("******");
            switch (sipServerInfoBase.getTransportType()) {
                case 3:
                    sipTransTypeGroup.check(R.id.sip_radio_button_auto);
                    break;
                case 2:
                    sipTransTypeGroup.check(R.id.sip_radio_button_tcp);
                    break;
                case 1:
                    sipTransTypeGroup.check(R.id.sip_radio_button_udp);
                    break;
                case 0:
                    sipTransTypeGroup.check(R.id.sip_radio_button_tls);
                    break;
            }
        } else {
            sipState.setText("未注册");
        }

        if (h323ServerInfoBase != null && h323ServerInfoBase.getH323RegState() == RegisterState.REG_STATE_REGISTERED) {
            h323State.setText("已注册");
            h323GkAddress.setText(h323ServerInfoBase.getServerAddr());
            h323E164.setText(h323ServerInfoBase.getSiteNumber());
            h323Account.setText(h323ServerInfoBase.getUserName());
            h323Password.setText("******");
        } else {
            h323State.setText("未注册");
        }
    }

    private void initConfConfig() {
        undisturbSwitch.setChecked(ConfSdkPresenter.isUndisturbState());
    }

    private void sipMoreClickHandle() {
        if (sipRegisterDetail.getVisibility() == View.GONE) {
            sipRegisterDetail.setVisibility(View.VISIBLE);
            sipMore.setImageResource(R.drawable.arrow_down);
        } else {
            sipRegisterDetail.setVisibility(View.GONE);
            sipMore.setImageResource(R.drawable.arrow_right);
        }
    }
    private void h323MoreClickHandle() {
        if (h323RegisterDetail.getVisibility() == View.GONE) {
            h323RegisterDetail.setVisibility(View.VISIBLE);
            h323More.setImageResource(R.drawable.arrow_down);
        } else {
            h323RegisterDetail.setVisibility(View.GONE);
            h323More.setImageResource(R.drawable.arrow_right);
        }
    }

    private void registSip() {
        String addr = sipServerAddress.getText().toString().trim();
        String proxyAddr = sipProxyServerAddress.getText().toString().trim();
        String Uri = sipUri.getText().toString().trim();
        String account = sipAccount.getText().toString().trim();
        String password = sipPassword.getText().toString().trim();
        RadioButton checkedButton = findViewById(sipTransTypeGroup.getCheckedRadioButtonId());
        int transType = Integer.parseInt(checkedButton.getTag().toString());

        if (StringUtil.isOverMaxLen(addr, 128)) {
            Toast.makeText(mContext, "服务器地址不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(proxyAddr, 128)) {
            Toast.makeText(mContext, "代理服务器地址不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(Uri, 128)) {
            Toast.makeText(mContext, "URI不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(account, 128)) {
            Toast.makeText(mContext, "SIP认证用户名不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(password, 32)) {
            Toast.makeText(mContext, "SIP认证密码不能超过32个字符", Toast.LENGTH_LONG).show();
            return;
        }
        SIPServerInfo sipServerInfo = new SIPServerInfo();
        sipServerInfo.setServerAddr(addr);
        sipServerInfo.setProxyServerAddr(proxyAddr);
        sipServerInfo.setSiteNumber(Uri);
        sipServerInfo.setUserName(account);
        sipServerInfo.setTransportType(transType);
        sipServerInfo.setChanged(false);
        if (!password.equals("******")) {
            sipServerInfo.setChanged(true);
            sipServerInfo.setPassword(password);
        }
        int result = ConfSdkPresenter.setSIPAccountInfo(sipServerInfo);
        Toast.makeText(mContext, result == 0 ? "注册中" : "注册失败", Toast.LENGTH_LONG).show();
    }

    private void registH323() {
        String gkAddr = h323GkAddress.getText().toString().trim();
        String e164 = h323E164.getText().toString().trim();
        String userName = h323Account.getText().toString().trim();
        String password = h323Password.getText().toString().trim();

        if (StringUtil.isOverMaxLen(gkAddr, 128)) {
            Toast.makeText(mContext, "GK地址不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(e164, 32)) {
            Toast.makeText(mContext, "E.164不能超过32个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(userName, 128)) {
            Toast.makeText(mContext, "H.323认证用户名不能超过128个字符", Toast.LENGTH_LONG).show();
            return;
        }
        if (StringUtil.isOverMaxLen(gkAddr, 32)) {
            Toast.makeText(mContext, "H.323认证密码不能超过32个字符", Toast.LENGTH_LONG).show();
            return;
        }

        H323ServerInfo h323ServerInfo = new H323ServerInfo();
        h323ServerInfo.setServerAddr(gkAddr);
        h323ServerInfo.setSiteNumber(e164);
        h323ServerInfo.setUserName(userName);
        h323ServerInfo.setChanged(false);
        if (!password.equals("******")) {
            h323ServerInfo.setChanged(true);
            h323ServerInfo.setPassword(password);
        }
        int result = ConfSdkPresenter.setH323AccountInfo(h323ServerInfo);
        Toast.makeText(mContext, result == 0 ? "注册中" : "注册失败", Toast.LENGTH_LONG).show();
    }

    private void exitClickHandle() {
        dismiss();
    }

    private void undisturbHandle(CompoundButton View, boolean isChecked) {
        if (isChecked) {
            ConfSdkPresenter.startUndisturb();
        } else {
            ConfSdkPresenter.stopUndisturb();
        }
    }
}
